import { computed } from "vue";
import { useRoute } from "vue-router";
import type { RouteRecordRaw } from "vue-router";

export function useMenuControl() {
  const route = useRoute();

  // 判断当前路由是否需要隐藏菜单
  const isMenuHidden = computed(() => {
    return (
      route.meta?.hideMenu ||
      ["/login", "/404"].includes(route.path) ||
      route.name === "ErrorPage"
    );
  });

  // 判断菜单项是否应该显示
  const shouldShowMenuItem = (item: RouteRecordRaw) => {
    // 检查权限
    if (item.meta?.permissions) {
      // TODO: 实现权限检查逻辑
      return true;
    }

    // 检查其他隐藏条件
    return !item.meta?.hidden && !item.meta?.hideMenu && item.meta?.title;
  };

  // 判断是否为外部链接
  const isExternalLink = (path: string) => {
    return /^(https?:|mailto:|tel:)/.test(path);
  };

  return {
    isMenuHidden,
    shouldShowMenuItem,
    isExternalLink,
  };
}
